<?php
class AlumnoactualizaController extends AppController {
    var $helpers = array('Html', 'Form','Session','Js');
    //se llama a las tablas con el nombre definido en el modelo
    var $uses = array('Notasemestrale', 'Alumno', 'Alumnosemestre');
    //el estilo por defecto
    var $layout = 'principal';
    
    function index(){
        //$this->layout= 'ajax';
        $alumnos = $this->Alumno->find('all');
        $notas = $this->Notasemestrale->find('all');
        
        
        foreach($alumnos as $alu){
            $id_alu = $alu['Alumno']['id'];
            
            $gestion = $notas[0]['Notasemestrale']['gestion'];
            
            
              //$notaalu = $this->Notasemestrale->find('all', array('conditions'=>array('Notasemestrale.alumno_id'=>$id_alu)));
              $gestiones = $this->Notasemestrale->find('all', array('conditions'=>array('Notasemestrale.alumno_id'=>$id_alu), 'group'=>'Notasemestrale.gestion', 'fields'=>'Notasemestrale.gestion'));
              //debug($gestiones);exit;
               
               foreach($gestiones as $g){
                  //if($g['Notasemestrale']['gestion'] == )
                  $nota = $this->Notasemestrale->find('count', array('conditions'=>array('Notasemestrale.alumno_id'=>$id_alu,'Notasemestrale.nota <'=>51, 'Notasemestrale.gestion'=> $g['Notasemestrale']['gestion'])));
                  //debug($nota);
                  $semestre = $this->Notasemestrale->find('first', array('conditions'=>array('Notasemestrale.gestion'=> $g['Notasemestrale']['gestion'], 'Notasemestrale.alumno_id'=>$id_alu)));
                  //debug($semestre);exit;
                  if($nota >=1){
                    $this->Alumnosemestre->create();
            
                $this->request->data['Alumnosemestre']['alumno_id'] = $id_alu;
                $this->request->data['Alumnosemestre']['semestre'] = $semestre['Materia']['semestre'];
                $nomsem = $this->getsemestremat($semestre['Materia']['semestre']);
                $this->request->data['Alumnosemestre']['nombre']= $nomsem;
                $this->request->data['Alumnosemestre']['carrera_id'] = $alu['Alumno']['carrera_id'];
                $this->request->data['Alumnosemestre']['gestion'] = $g['Notasemestrale']['gestion'];
                $this->request->data['Alumnosemestre']['estado_id']=2;
                //debug($this->data);
                $this->Alumnosemestre->save($this->data);
                  }else{
                    $this->Alumnosemestre->create();
                    $this->request->data['Alumnosemestre']['alumno_id'] = $id_alu;
                    $this->request->data['Alumnosemestre']['semestre'] = $semestre['Materia']['semestre'];
                    $nomsem = $this->getsemestremat($semestre['Materia']['semestre']);
                    $this->request->data['Alumnosemestre']['nombre']= $nomsem;
                    $this->request->data['Alumnosemestre']['carrera_id'] = $alu['Alumno']['carrera_id'];
                    $this->request->data['Alumnosemestre']['gestion'] = $g['Notasemestrale']['gestion'];
                    $this->request->data['Alumnosemestre']['estado_id']=1;
                //debug($this->data);
                $this->Alumnosemestre->save($this->data);
                  }
               }
               
                }
                $this->Session->setFlash('Se registro con exito los estados del alumno!!!!!');
            }

          

  } 

    

?>